package org.itboys.ace.web.lasted;

import org.itboys.admin.service.lasted.SystemRolePermissionService;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;

import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *作者：江雄
 * 描述：系统-角色—权限
 * 时间：2017年2月24日.重新构建
 */
@RestController
@RequestMapping("/ace/system/role/permission")
public class AceSystemRolePermissionController extends BaseController{
    @Autowired
    private SystemRolePermissionService rolePermissionService;

    /**
     * 保存
     * @param request
     * @param response
     */
    @RequestMapping(value="/saveRolePermission", method=RequestMethod.POST)
    public void saveRolePermission(@RequestParam(value="roleId",required =true)Long roleId,
                              @RequestParam(value="permissionIds",required =true)List<Long> permissionIds,
                              @RequestParam(value="types",required =true)List<Integer> types,
                              HttpServletRequest request, HttpServletResponse response) {
        try{

            rolePermissionService.saveRolePermission(roleId,permissionIds,types);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }catch(Exception e){
            logger.error("SystemUser save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 获取已有的菜单权限
     * @param request
     * @param response
     */
    @RequestMapping(value="/allotRolePerMenu", method=RequestMethod.POST)
    public void allotRolePerMenu(@RequestParam(value="roleId",required =true)Long roleId, HttpServletRequest request, HttpServletResponse response) {
        try{
            // userRoleService.allotUserRole(userId);
            AjaxUtils.renderJson(response,rolePermissionService.allotRolePerMenu(roleId));
        }catch(Exception e){
            logger.error("SystemUser save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 获取已有的菜单功能权限
     * @param request
     * @param response
     */
    @RequestMapping(value="/allotRolePerMenuOperate", method=RequestMethod.POST)
    public void allotRolePerMenuOperate(@RequestParam(value="roleId",required =true)Long roleId, HttpServletRequest request, HttpServletResponse response) {
        try{
            // userRoleService.allotUserRole(userId);
            AjaxUtils.renderJson(response,rolePermissionService.allotRolePerMenuOperate(roleId));
        }catch(Exception e){
            logger.error("SystemUser save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }



}
